package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.system.service.ISysUserService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.TbTaskReportingdetailBo;
import com.ruoyi.tb.lmt.domain.vo.TbTaskReportingdetailVo;
import com.ruoyi.tb.lmt.domain.TbTaskReportingdetail;
import com.ruoyi.tb.lmt.mapper.TbTaskReportingdetailMapper;
import com.ruoyi.tb.lmt.service.ITbTaskReportingdetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 任务报工详情Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-10
 */
@RequiredArgsConstructor
@Service
public class TbTaskReportingdetailServiceImpl implements ITbTaskReportingdetailService {

    private final TbTaskReportingdetailMapper baseMapper;

    private final ISysUserService sysUserService;

    /**
     * 查询任务报工详情
     */
    @Override
    public TbTaskReportingdetailVo queryById(Long ID){
        return baseMapper.selectVoById(ID);
    }

    /**
     * 查询任务报工详情列表
     */
    @Override
    public TableDataInfo<TbTaskReportingdetailVo> queryPageList(TbTaskReportingdetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TbTaskReportingdetail> lqw = buildQueryWrapper(bo);
        Page<TbTaskReportingdetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        for (TbTaskReportingdetailVo vo : result.getRecords()) {
            if (StringUtils.isNotBlank(vo.getCreateBy())){
                SysUser sysUser = sysUserService.selectUserByUserName(vo.getCreateBy());
                if (sysUser!=null){
                    vo.setCreateByName(sysUser.getNickName());
                }
            }
        }
        return TableDataInfo.build(result);
    }

    /**
     * 查询任务报工详情列表
     */
    @Override
    public List<TbTaskReportingdetailVo> queryList(TbTaskReportingdetailBo bo) {
        LambdaQueryWrapper<TbTaskReportingdetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TbTaskReportingdetail> buildQueryWrapper(TbTaskReportingdetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TbTaskReportingdetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getWorkType()), TbTaskReportingdetail::getWorkType, bo.getWorkType());
        lqw.eq(StringUtils.isNotBlank(bo.getFormConfig()), TbTaskReportingdetail::getFormConfig, bo.getFormConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getFillData()), TbTaskReportingdetail::getFillData, bo.getFillData());
        lqw.eq(StringUtils.isNotBlank(bo.getPrintConfig()), TbTaskReportingdetail::getPrintConfig, bo.getPrintConfig());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TbTaskReportingdetail::getStatus, bo.getStatus());
        lqw.eq(bo.getCreateTime() != null, TbTaskReportingdetail::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), TbTaskReportingdetail::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getUpdateTime() != null, TbTaskReportingdetail::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), TbTaskReportingdetail::getUpdateBy, bo.getUpdateBy());
        return lqw;
    }

    /**
     * 新增任务报工详情
     */
    @Override
    public Boolean insertByBo(TbTaskReportingdetailBo bo) {
        TbTaskReportingdetail add = BeanUtil.toBean(bo, TbTaskReportingdetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setID(add.getID());
        }
        return flag;
    }

    /**
     * 修改任务报工详情
     */
    @Override
    public Boolean updateByBo(TbTaskReportingdetailBo bo) {
        TbTaskReportingdetail update = BeanUtil.toBean(bo, TbTaskReportingdetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TbTaskReportingdetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除任务报工详情
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
